package com.yangye.picommon.model;

import com.yangye.picommon.enums.PiResultEnum;

/**
 * @author Page.Yang
 * @date 2019/4/3 0003
 */
public class PiResult<T> {

    private int code;
    private String message;
    private T data;

    private PiResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private PiResult(int code, T data) {
        this.code = code;
        this.data = data;
    }

    private PiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功（不需要返回结果集）
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> succeed() {
        return new PiResult<T>(PiResultEnum.SUCCEED.getCode(), PiResultEnum.SUCCEED.getMsg());
    }

    /**
     * 成功（返回结果）
     * @param data
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> succeed(T data) {
        return new PiResult<T>(PiResultEnum.SUCCEED.getCode(), data);
    }

    /**
     * 失败
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> fail() {
        return new PiResult<T>(PiResultEnum.FAIL.getCode(), PiResultEnum.FAIL.getMsg());
    }

    /**
     * 失败
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> PiResult fail(int code, String msg) {
        return new PiResult<T>(code, msg);
    }

    public static <T> PiResult fail(PiResultEnum piResultEnum) {
        return new PiResult<T>(piResultEnum.getCode(), piResultEnum.getMsg());
    }

    /**
     * 失败（返回结果）
     * @param data
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> fail(T data) {
        return new PiResult<T>(PiResultEnum.FAIL.getCode(), PiResultEnum.FAIL.getMsg(), data);
    }

    /**
     * 失败（返回失败消息）
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> fail(String msg) {
        return new PiResult<T>(PiResultEnum.FAIL.getCode(), msg, null);
    }

    /**
     * 未登录
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> unauthorized() {
        return new PiResult<T>(PiResultEnum.UNAUTHORIZED.getCode(), PiResultEnum.UNAUTHORIZED.getMsg());
    }

    /**
     * token错误
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> badToken() {
        return new PiResult<T>(PiResultEnum.BAD_TOKEN.getCode(), PiResultEnum.BAD_TOKEN.getMsg());
    }

    /**
     * token过期
     * @param <T>
     * @return
     */
    public static <T> PiResult<T> tokenExpired() {
        return new PiResult<T>(PiResultEnum.TOKEN_EXPIRED.getCode(), PiResultEnum.TOKEN_EXPIRED.getMsg());
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
